package com.lyl.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author lylbell
 * @date 2021-06-29 15:07
 */
@EnableConfigurationProperties(IdProperties.class)
@Configuration
public class IdAutoConfiguration {

    private static IdProperties idProperties;

    private static RestTemplate restTemplate = new RestTemplate();

    public IdAutoConfiguration(IdProperties idProperties) {
        IdAutoConfiguration.idProperties = idProperties;
    }

    /**
     * 用户信息的实名认证
     * @param realName
     * @param idCard
     * @return
     */
    public static boolean check(String realName,String idCard) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization","APPCODE " + idProperties.getAppCode());
        ResponseEntity<String> exchange = restTemplate.exchange(
                //%s 是变量,
                String.format(idProperties.getUrl(), idCard, realName),
                HttpMethod.GET,
                new HttpEntity<>(null, httpHeaders),
                String.class
        );
        if (exchange.getStatusCode() == HttpStatus.OK) {
            String body = exchange.getBody();
            JSONObject jsonObject = JSON.parseObject(body);
            String status = jsonObject.getString("status");
            if ("01".equals(status)) {
                return true;
            }
        }
        return false;
    }



}
